﻿using System;
using System.Web;
using System.Web.Security;
using Zoo.Cat.Application.Jobseekers;
using Zoo.Cat.Domain.Entities.Jobseekers;

namespace Zoo.Cat.Application.Authentication {
    public partial class FormsAuthenticationService : IAuthenticationService {
        private readonly HttpContextBase _httpContext;
        private readonly IJobseekerService _JobseekerService;       
        private readonly TimeSpan _expirationTimeSpan;

        private Jobseeker _cachedJobseeker;

        public FormsAuthenticationService(HttpContextBase httpContext,
            IJobseekerService JobseekerService) {
                this._httpContext = httpContext;
                this._JobseekerService = JobseekerService;
                this._expirationTimeSpan = FormsAuthentication.Timeout;
        }

        public virtual void SignIn(Jobseeker Jobseeker, bool createPersistentCookie) {
            var now = DateTime.Now;

            var ticket = new FormsAuthenticationTicket(
                1 /*version*/,
                Jobseeker.Email,
                now,
                now.Add(_expirationTimeSpan),
                createPersistentCookie,
                Jobseeker.Email,
                FormsAuthentication.FormsCookiePath);

            var encryptedTicket = FormsAuthentication.Encrypt(ticket);

            var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);
            cookie.HttpOnly = true;
            if (ticket.IsPersistent) {
                cookie.Expires = ticket.Expiration;
            }
            cookie.Secure = FormsAuthentication.RequireSSL;
            cookie.Path = FormsAuthentication.FormsCookiePath;
            if (FormsAuthentication.CookieDomain != null) {
                cookie.Domain = FormsAuthentication.CookieDomain;
            }

            _httpContext.Response.Cookies.Add(cookie);
            _cachedJobseeker = Jobseeker;
        }


        public virtual void SignOut() {
            _cachedJobseeker = null;
            FormsAuthentication.SignOut();
        }

        public virtual Jobseeker GetAuthenticatedUser() {
            if (_cachedJobseeker != null)
                return _cachedJobseeker;

            if (_httpContext == null ||
                _httpContext.Request == null ||
                !_httpContext.Request.IsAuthenticated ||
                !(_httpContext.User.Identity is FormsIdentity)) {
                return null;
            }

            var formsIdentity = (FormsIdentity)_httpContext.User.Identity;
            var Jobseeker = GetAuthenticatedUserFromTicket(formsIdentity.Ticket);
            if (Jobseeker != null && Jobseeker.Active)
                _cachedJobseeker = Jobseeker;
            return _cachedJobseeker;
        }


        public virtual Jobseeker GetAuthenticatedUserFromTicket(FormsAuthenticationTicket ticket) {
            if (ticket == null)
                throw new ArgumentNullException("ticket");

            var email = ticket.UserData;

            if (String.IsNullOrWhiteSpace(email))
                return null;
            var Jobseeker = _JobseekerService.GetJobseekerByEmail(email);
            return Jobseeker;
        }


    }
}
